home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1996, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
-
- /* fog_offset.c
- * This program demonstrates using fog offset to make a fogged
- * object appear brighter.
- *
- * If the fog mode is GL_EXP or GL_EXP2, pressing the up and down
- * arrow keys adjusts the density value.
- *
- * If the fog mode is GL_LINEAR, pressing the up and down
- * arrow keys adjusts the end value for linear fog. The start value
- * for the linear fog is fixed.
- *
- * Escape key - exit the program
- * <o> key - toggle fog offset
- * <f> key - change the fog blend function
- * UP Arrow Key - increase the fog density or end value
- * DOWN Arrow Key - decrease the fog density or end value
- */
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include <GL/glut.h>
-
- #include <math.h>
- #include <stdio.h>
- #include <stdlib.h>
-
- /* Function Prototypes */
-
- GLvoid initgfx( GLvoid );
- GLvoid drawScene( GLvoid );
- GLvoid reshape( GLsizei, GLsizei );
- GLvoid keyboard( GLubyte, GLint, GLint );
- GLvoid specialkeys( GLint, GLint, GLint );
-
- GLvoid cycleFog( GLvoid );
- GLvoid inc( GLvoid );
- GLvoid dec( GLvoid );
- GLvoid toggleFogOffsetEnable( GLvoid );
-
- void printHelp( char * );
-
- /* Global Definitions */
-
- #define PROJ_LEFT 0.0
- #define PROJ_RIGHT 511.0
- #define PROJ_BOTTOM 0.0
- #define PROJ_TOP 511.0
-
- #define KEY_ESC 27 /* ascii value for the escape key */
-
- /* Global Variables */
-
- static GLint fogMode;
- static GLfloat fogDensity;
- static GLfloat fogStart = 5.0;
- static GLfloat fogEnd = 11.0;
- GLfloat fogColor[4] = { 0.0, 0.0, 1.0, 1.0 };
-
- static GLboolean fogOffsetSupported = GL_TRUE;
- static GLboolean fogOffsetEnable = GL_FALSE;
- static GLfloat fogOffset[4];
-
-
- static float projNearTolerance, projFarTolerance;
-
- static GLfloat x, y;
-
- static GLfloat angle = 80.0;
-
- void
- main( int argc, char *argv[] )
- {
- GLsizei width, height;
-
- glutInit( &argc, argv );
-
- width = glutGet( GLUT_SCREEN_WIDTH );
- height = glutGet( GLUT_SCREEN_HEIGHT );
- glutInitWindowPosition( width/4, height/4 );
- glutInitWindowSize( width/2, height/2 );
- glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
- glutCreateWindow( argv[0] );
-
- initgfx();
-
- glutKeyboardFunc( keyboard );
- glutSpecialFunc( specialkeys );
- glutReshapeFunc( reshape );
- glutDisplayFunc( drawScene );
-
- printHelp( argv[0] );
- printf("\nFog mode is GL_EXP, density = %4.2f\n", fogDensity);
-
- if (!glutExtensionSupported("GL_SGIS_fog_func")) {
- fogOffsetSupported = GL_FALSE;
- fprintf( stderr,
- "GL_SGIS_fog_offset not supported on this machine\n");
- }
- glutMainLoop();
- }
-
- GLvoid
- printHelp( char *progname )
- {
- fprintf(stdout,"\n%s - demonstrates fog\n\n"\
- "Escape key - exit the program\n"
- "<f> key - change fog blend function\n"
- "<o> key - toggle fog offset\n"
- "UP Arrow Key - increase fog density or end value\n"
- "DOWN Arrow Key - decrease fog density or end value\n",
- progname);
- }
-
- /* Initialize lighting and materials, enable depth buffer, and set
- * initial fog parameters.
- */
- GLvoid
- initgfx( GLvoid )
- {
-
- /* set the fog function and density */
- fogMode = GL_EXP;
- fogDensity = 0.25;
-
- /* setup and enable fog */
- glFogi( GL_FOG_MODE, fogMode );
- glHint( GL_FOG_HINT, GL_DONT_CARE );
- }
-
- GLvoid
- cycleFog( GLvoid )
- {
- if (fogMode == GL_EXP) {
- fogMode = GL_EXP2;
- printf( "Fog mode is GL_EXP2\n" );
- } else if (fogMode == GL_EXP2) {
- fogMode = GL_LINEAR;
- printf( "Fog mode is GL_LINEAR\n" );
- } else if (fogMode == GL_LINEAR) {
- fogMode = GL_EXP;
- printf( "Fog mode is GL_EXP\n" );
- }
- glFogi( GL_FOG_MODE, fogMode );
- }
-
- GLvoid
- dec( GLvoid )
- {
- if (fogMode == GL_LINEAR) {
- fogEnd -= 0.5;
- if (fogEnd < fogStart) fogEnd = fogStart;
- glFogf( GL_FOG_END, fogEnd );
- printf( "Fog end = %4.2f\n", fogEnd );
- } else {
- fogDensity -= 0.05;
- if (fogDensity < 0.0) fogDensity = 0.0;
- glFogf( GL_FOG_DENSITY, fogDensity );
- printf( "Fog density = %4.2f\n", fogDensity );
- }
- }
-
- GLvoid
- inc( GLvoid )
- {
- if (fogMode == GL_LINEAR) {
- fogEnd += 0.5;
- if (fogEnd > 12.0) fogEnd = 12.0;
- glFogf( GL_FOG_END, fogEnd );
- printf( "Fog end = %4.2f\n", fogEnd );
- } else {
- fogDensity += 0.05;
- if (fogDensity > 1.0) fogDensity = 1.0;
- glFogf (GL_FOG_DENSITY, fogDensity);
- printf ("Fog density = %4.2f\n", fogDensity);
- }
- }
-
- GLvoid
- specialkeys( GLint key, GLint x, GLint y )
- {
- switch (key) {
- case GLUT_KEY_UP: /* increase fog density or end */
- inc();
- break;
- case GLUT_KEY_DOWN: /* decrease fog density or end */
- dec();
- break;
- }
- glutPostRedisplay();
- }
-
- void toggleFogOffsetEnable( GLvoid )
- {
- if (fogOffsetSupported) {
- fogOffsetEnable = !fogOffsetEnable;
- printf("Fog offset %s\n",
- (fogOffsetEnable? "Enabled":"Disabled"));
- } else
- printf("GL_SGIX_fog_offset not supported\n");
- }
-
- GLvoid
- keyboard( GLubyte key, GLint x, GLint y )
- {
- switch (key) {
- case 'f': /* cycle fog mode */
- cycleFog();
- glutPostRedisplay();
- break;
- case 'o': /* toggle fog offset enable */
- toggleFogOffsetEnable();
- glutPostRedisplay();
- break;
- case KEY_ESC: /* Exit whenever the Escape key is pressed */
- exit(0);
- }
- }
-
- GLvoid
- reshape( GLsizei width, GLsizei height )
- {
- GLdouble aspect;
-
- glViewport( 0, 0, width, height );
-
- aspect = (GLdouble) width / (GLdouble) height;
- }
-
- #define DBG2 0
-
- #define FOG_STEP 0.05
-
- #define EPSILON 0.999999
- #define TOLERANCE 1
-
- #define X_STEP 12.0
- #define Y_STEP 12.0
-
- static void setProjectionAndFog(GLfloat dist)
- {
-
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
-
- glOrtho(PROJ_LEFT, PROJ_RIGHT, PROJ_BOTTOM, PROJ_TOP, -dist, dist);
-
- projNearTolerance = (-dist > 0) ? - -dist * (1.0 + EPSILON) : 0;
- projFarTolerance = - dist * EPSILON;
-
- glFogf(GL_FOG_END, dist);
- }
-
- static void setFogOffsetEnable(int enable)
- {
- if (!fogOffsetSupported) return;
-
- fogOffsetEnable = enable;
- #ifdef GL_SGIX_fog_offset
- if (fogOffsetEnable)
- glEnable(GL_FOG_OFFSET_SGIX);
- else
- glDisable(GL_FOG_OFFSET_SGIX);
- #else
- printf("GL_SGIX_fog_offset not supported\n");
- #endif
- }
-
- static void setFogOffsetValue(float value)
- {
- if (!fogOffsetSupported) return;
-
- fogOffset[3] = value;
- #ifdef GL_SGIX_fog_offset
- glFogfv(GL_FOG_OFFSET_VALUE_SGIX, fogOffset);
- #else
- printf("GL_SGIX_fog_offset not supported\n");
- #endif
- }
-
- static void doPoint(GLfloat z)
- {
- glBegin(GL_POINTS);
- glVertex3d(x, y, z);
- glEnd();
- }
-
- static GLfloat zFromF(GLfloat f)
- {
- GLfloat z;
-
- z = f * (fogEnd - fogStart) - fogEnd;
-
- if (z < projFarTolerance)
- z = projFarTolerance;
- if (z > projNearTolerance)
- z = projNearTolerance;
-
- if (z > 0)
- z = 0;
- #if DBG2
- fprintf(stderr, "f %f z %f \n", f, z);
- #endif
- return(z);
- }
-
- static void draw(void)
- {
- GLfloat f;
-
- x = PROJ_LEFT + X_STEP;
- f = 1.0;
- while(f > 0.0){
- doPoint(zFromF(f));
- f -= FOG_STEP;
- x += X_STEP;
- }
- doPoint(zFromF(0.0));
- y += Y_STEP;
- }
-
- GLvoid
- drawScene( GLvoid )
- {
- GLfloat fOff;
-
- glClearColor(0.2, 0.2, 0.2, 0.2);
- glClear(GL_COLOR_BUFFER_BIT);
-
- glPointSize(10.0);
- glColor4f(1.0, 1.0, 1.0, 1.0);
-
- glEnable(GL_FOG);
- glFogfv(GL_FOG_COLOR, fogColor);
- glFogi(GL_FOG_MODE, GL_LINEAR);
- fogStart = 0.0;
- glFogf(GL_FOG_START, fogStart);
-
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
-
- fogOffsetEnable = 0;
- fogOffset[0] = 0;
- fogOffset[1] = 0;
- fogOffset[2] = -1.0;
- fogOffset[3] = 0.0;
-
- y = PROJ_BOTTOM + Y_STEP;
-
- /* set projection matrix; fog offset disable */
- setProjectionAndFog(100.0);
-
- /* set fog offset value; fog offset disable */
- setFogOffsetValue(19.0);
- draw();
-
- /* enalbe fog offset */
- setFogOffsetEnable(1);
- draw();
-
- /* modify fog offset value; fog offset enable */
- setFogOffsetValue(30.0);
- draw();
-
- /* disable fog offset */
- setFogOffsetEnable(0);
- draw();
-
- /* modify projection matrix; fog offset disable */
- setProjectionAndFog(500.0);
- draw();
- /* enable fog offset after projection matrix had been modified */
- setFogOffsetEnable(1);
- draw();
- /* modify projection matrix; fog offset enable */
- setProjectionAndFog(1000.0);
- draw();
- /* modify fog offset value; fog offset enable */
- setFogOffsetValue(150.0);
- draw();
- /* modify fog offset value; fog offset enable */
- setFogOffsetValue(250.0);
- draw();
- /*disable fog, modify projection matrix, enable fog; fog offset enable*/
- glDisable(GL_FOG);
- setProjectionAndFog(10000.0);
- glEnable(GL_FOG);
- draw();
- /* disable fog offset */
- setFogOffsetEnable(0);
- draw();
- /* modify fog offset value to 0.0 and enable fog offset */
- setFogOffsetEnable(1);
- setFogOffsetValue(0.0);
- draw();
- for (fOff = 50.0; fOff <= 20000.0; fOff *= 1.7){
- setFogOffsetValue(fOff);
- draw();
- }
- setFogOffsetEnable(0);
-
- glutSwapBuffers();
-
- checkError("drawScene");
- }
-